home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_404_4.txt < prev    next >
Text File  |  1997-01-29  |  3KB  |  115 lines

  1. FLOW CONTROL : syntax, examples
  2. _________________________________________________________________________
  3.  
  4.  
  5. CONTROL STATEMENT SYNTAX
  6.  
  7. There are several control statements available in C.  They are; 'while', 'for', 'do', 'if', conditional, 'switch', and the infamous 'goto'.  Syntax for these statements are:
  8.  
  9.   while( expr )
  10.     statement
  11.  
  12.   for( init; test; update )
  13.     statement
  14.  
  15.   do                              // statement executed at least once
  16.     statement
  17.   while( expr );
  18.  
  19.   if( expr )
  20.     statement
  21.   else if( expr )
  22.     statement
  23.   else
  24.     statement
  25.  
  26.   expr ? expr : expr              // conditional
  27.  
  28.   switch( expr )
  29.   {
  30.     case const1: statement
  31.                  break;           // without break, control
  32.     case const2: statement        // continues to next 
  33.                  break;           // statement.
  34.     case const3: statement
  35.                  break;
  36.     default    : statement
  37.   }
  38.  
  39.   goto label;
  40.   .
  41.   .
  42.   .
  43.   label:statement
  44.  
  45.  
  46.  
  47. FLOW CONTROL EXAMPLES
  48.  
  49. The following program provides some basic examples of each control statement.  Although the goto statement is provided in C, it is usually avoided and most books advise against its use.  The conditional statement is shorthand for an if-then statement, but it can decrease code readability (so it is also not recommended).
  50.  
  51.   // control.cp
  52.   #include <iostream.h>
  53.  
  54.   void main()
  55.   {
  56.     int a = 1;
  57.     int b = 2;
  58.     int i = 1;
  59.  
  60.     cout << "start while loop" << endl;   // while
  61.     while( i<4 )
  62.     {
  63.       cout << "i = " << i << endl;
  64.       i++;
  65.     }
  66.  
  67.     cout << "start for loop" << endl;     // for
  68.     for( i = 1; i<4; i++ )
  69.       cout << "i = " << i << endl;
  70.  
  71.     cout << "start do loop" << endl;      // do
  72.     i = 1;
  73.     do
  74.     {
  75.       cout << "i = " << i << endl;
  76.       i++;
  77.     }
  78.     while( i<4 );
  79.  
  80.     cout << "start if" << endl;           // if
  81.     i = 2;
  82.     if( i==a )
  83.       cout << "answer is a" << endl;
  84.     else if( i==b )
  85.       cout << "answer is b" << endl;
  86.     else
  87.       cout << "neither a or b" << endl;
  88.  
  89.     cout << "conditional" << endl;        // conditional
  90.     i = 10;
  91.     i = ( i>b ) ? b : i;
  92.     cout << "i = " << i << endl;
  93.  
  94.     cout << "switch statement" << endl;   // switch
  95.     switch( i )
  96.     {
  97.       case 1:  cout << "one\n";
  98.                break;
  99.       case 2:  cout << "two\n";
  100.                break;
  101.       case 3:  cout << "three\n";
  102.                break;
  103.       default: cout << "error\n";
  104.     }
  105.  
  106.     cout << "goto statement" << endl;     // goto
  107.     if( i==b )
  108.       goto skip; // functional scope
  109.     cout << "zero\n";
  110.     cout << "one\n";
  111.     skip:
  112.     cout << "two\n";
  113.     cout << "three\n";
  114.   }
  115.   // end control.cp